package com.yohann.exam_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yohann.exam_service.entity.Listening;
import com.yohann.exam_service.entity.vo.ListeningVO;
import com.yohann.exam_service.mapper.ListeningMapper;
import com.yohann.exam_service.service.ListeningService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yohann.utils.result.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 听力表 服务实现类
 * </p>
 *
 * @author Yohann
 * @since 2020-11-19
 */
@Service
public class ListeningServiceImpl extends ServiceImpl<ListeningMapper, Listening> implements ListeningService {

    /**
     * 删除听力题目
     *
     * @param id 题目id
     * @return 删除结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteListening(int id) {
        //查询子节点
        LambdaQueryWrapper<Listening> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Listening::getParentId, id);
        List<Listening> nodeList = baseMapper.selectList(wrapper);

        if (nodeList.size() > 0) {
            //筛选出数据的id值
            List<Integer> ids = nodeList.stream().map(Listening::getId).collect(Collectors.toList());

            //如果存在子节点 就先删除子节点
            baseMapper.deleteBatchIds(ids);
        }

        //删除传入节点
        baseMapper.deleteById(id);

        return true;
    }

    /**
     * 查询听力题目
     *
     * @param id      题目父id
     * @param section 题目章节
     * @return 结果集
     */
    @Override
    public Result queryListening(Integer id, Integer section) {
        //如果类型为空 则查询所有子节点
        if (section == null) {
            return queryListening(id);
        }

        //获取章节信息
        List<Listening> list = getSection(id, section);

        return Result.ok().data("list", list);
    }

    /**
     * 查询听力题目所有子节点
     *
     * @param id 题目父id
     * @return 结果集
     */
    private Result queryListening(Integer id) {
        ListeningVO vo = new ListeningVO();

        //查询sectionA
        vo.setSectionA(getSection(id, 1));
        //查询sectionB
        vo.setSectionB(getSection(id, 2));
        //查询sectionC
        vo.setSectionC(getSection(id, 3));

        return Result.ok().data("list", vo);
    }

    /**
     * 获取章节信息
     *
     * @param id      父id
     * @param section 章节类型
     * @return 结果集
     */
    private List<Listening> getSection(Integer id, Integer section) {
        //查询父id 章节符合的题目 升序排列
        LambdaQueryWrapper<Listening> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Listening::getParentId, id)
                .eq(Listening::getSection, section)
                .orderByAsc(Listening::getSort);

        return baseMapper.selectList(wrapper);
    }

    @Override
    public Map<String, Object> pageQueryListen(int current, int limit, String title) {
        LambdaQueryWrapper<Listening> wrapper = new LambdaQueryWrapper<>();
        //只查询父级
        wrapper.eq(Listening::getParentId, 0);
        if (title != null && !"".equals(title)) {
            wrapper.like(Listening::getTitle, title);
        }

        Page<Listening> page = new Page<>(current, limit);
        this.page(page, wrapper);

        //将查询到的总条数、结果集存入map中并返回
        Map<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("rows", page.getRecords());

        return map;
    }

    @Override
    public ListeningVO getListeningDetail(int id) {
        ListeningVO vo = new ListeningVO();

        //查询基本信息
        vo.setListening(getById(id));
        //查询sectionA
        vo.setSectionA(getSection(id, 1));
        //查询sectionB
        vo.setSectionB(getSection(id, 2));
        //查询sectionC
        vo.setSectionC(getSection(id, 3));

        return vo;
    }
}
